package com.hub.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

/**
 *@author hub
 */
@Getter
@Setter
public class ApiResultObject<T> {
    private int code;
    private String message;
    private T data;

    protected ApiResultObject(int code,String message,T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 是否成功标识
     *
     * @return
     */
    @JsonIgnore
    public boolean isSuccess() {
        return code == ApiCodeEnum.SUCCESS.getCode();
    }

    /**
     * 执行失败
     * @param <T>
     * @return
     */
    public static <T> ApiResultObject<T> failed() {
        return new ApiResultObject<T>(ApiCodeEnum.AuthFail.getCode(), ApiCodeEnum.AuthFail.getDesc(),null);
    }

    /**
     * 执行失败
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ApiResultObject<T> failed(int code,String message) {
        return new ApiResultObject<T>(code,message,null);
    }

    /**
     * 执行失败
     * @param apiCodeEnum
     * @param <T>
     * @return
     */
    public static <T> ApiResultObject<T> failed(ApiCodeEnum apiCodeEnum) {
        return new ApiResultObject<T>(apiCodeEnum.getCode(),apiCodeEnum.getDesc(),null);
    }

    /**
     * 执行失败
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ApiResultObject<T> failed(String message) {
        return new ApiResultObject<T>(ApiCodeEnum.FAILED.getCode(),message,null);
    }

    /**
     * 执行成功
     * @return
     */
    public static <T> ApiResultObject<T> success() {
        return  new ApiResultObject<T>(ApiCodeEnum.SUCCESS.getCode(),null,null);
    }

    /**
     * 执行成功
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ApiResultObject<T> success(String message ) {
        return  new ApiResultObject<T>(ApiCodeEnum.SUCCESS.getCode(),message,null);
    }

    /**
     * message
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ApiResultObject<T> success(T data ) {
        return new ApiResultObject<T>(ApiCodeEnum.SUCCESS.getCode(),"Success",data);
    }

    /**
     * 执行成功
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ApiResultObject<T> success(int code,String message) {
        return  new ApiResultObject<T>(code,message,null);
    }

    /**
     * 执行成功
     * @param code
     * @param message
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ApiResultObject<T> success(int code,String message,T data) {
        return  new ApiResultObject<T>(code,message,data);
    }

    @Override
    public String toString() {
        return "{" +
                "\"code\":" + code +
                ", \"message\":\"" + message + '\"' +
                ", \"data\":\"" + data + '\"'+
                '}';
    }
}
